#include "map_window.h"
#include "ui_map_window.h"
#include "main_window.h"

extern Main_Window *pmain_window;
Map_window::Map_window(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Map_window)
{
    ui->setupUi(this);
    ui->Back_menu->installEventFilter(this);
    ui->change->installEventFilter(this);
    map_show();
    chang_txt();
    timer = new QTimer(this);
    connect(timer,&QTimer::timeout,[this]
   {
       chang_txt();
   });
    timer->start(1000);
}
void Map_window::map_show(){
    QPixmap *pax = new QPixmap(":/picture/main_window/返回菜单.png");
    ui->Back_menu->setPixmap(*pax);
    ui->Back_menu->setScaledContents(true);

    ui->city->setStyleSheet("QLabel{color:white};");
    ui->temp_now->setStyleSheet("QLabel{color:white};");
    ui->tq_txt->setStyleSheet("QLabel{color:white};");

    QPixmap pic(":/picture/main_window/weather/晴天.png");
    ui->tq_pic->setPixmap(pic);
    ui->tq_pic->setScaledContents(true);

    pic.load(":/picture/weather_window/切换.png");
    ui->change->setPixmap(pic);
    ui->change->setScaledContents(true);

    pic.load(":/picture/weather_window/气球.png");
    ui->pict_1->setPixmap(pic);
    ui->pict_1->setScaledContents(true);

    pic.load(":/picture/weather_window/太阳伞.png");
    ui->pict_2->setPixmap(pic);
    ui->pict_2->setScaledContents(true);
    QPalette pex;
    pex.setColor(QPalette::Background ,QColor(53,154,225));       //背景色
    ui->label_1->setAutoFillBackground(true);
    ui->label_1->setPalette(pex);
    ui->label_1->setStyleSheet("QLabel{background-color: rgb(53,154,225);border:2px;border-radius:10px;border-color:rgb(53,154,225)};");
    ui->time1->setStyleSheet("QLabel{color:white};");
    ui->weather1->setStyleSheet("QLabel{color:white};");
    ui->tem1_h->setStyleSheet("QLabel{color:white};");
    ui->tem1_l->setStyleSheet("QLabel{color:white};");
    ui->tem1_y->setStyleSheet("QLabel{color:white};");
    QPixmap pax_1;
    pax_1.load(":/picture/main_window/weather/晴天.png");
    ui->picture1->setPixmap(pax_1);
    ui->picture1->setScaledContents(true);

    pex.setColor(QPalette::Background ,QColor(53,154,225));       //背景色
    ui->label_2->setAutoFillBackground(true);
    ui->label_2->setPalette(pex);
    ui->label_2->setStyleSheet("QLabel{background-color: rgb(53,154,225);border:2px;border-radius:10px;border-color:rgb(53,154,225)};");
    ui->time1_2->setStyleSheet("QLabel{color:white};");
    ui->weather1_2->setStyleSheet("QLabel{color:white};");
    ui->tem1_h_2->setStyleSheet("QLabel{color:white};");
    ui->tem1_l_2->setStyleSheet("QLabel{color:white};");
    ui->tem1_y_2->setStyleSheet("QLabel{color:white};");
    QPixmap pax_2;
    pax_2.load(":/picture/main_window/weather/晴天.png");
    ui->picture1_2->setPixmap(pax_2);
    ui->picture1_2->setScaledContents(true);

    pex.setColor(QPalette::Background ,QColor(53,154,225));       //背景色
    ui->label_3->setAutoFillBackground(true);
    ui->label_3->setPalette(pex);
    ui->label_3->setStyleSheet("QLabel{background-color: rgb(53,154,225);border:2px;border-radius:10px;border-color:rgb(53,154,225)};");
    ui->time1_3->setStyleSheet("QLabel{color:white};");
    ui->weather1_3->setStyleSheet("QLabel{color:white};");
    ui->tem1_h_3->setStyleSheet("QLabel{color:white};");
    ui->tem1_l_3->setStyleSheet("QLabel{color:white};");
    ui->tem1_y_3->setStyleSheet("QLabel{color:white};");
    QPixmap pax_3;
    pax_3.load(":/picture/main_window/weather/晴天.png");
    ui->picture1_3->setPixmap(pax_3);
    ui->picture1_3->setScaledContents(true);

    pex.setColor(QPalette::Background ,QColor(53,154,225));       //背景色
    ui->label_4->setAutoFillBackground(true);
    ui->label_4->setPalette(pex);
    ui->label_4->setStyleSheet("QLabel{background-color: rgb(53,154,225);border:2px;border-radius:10px;border-color:rgb(53,154,225)};");
    ui->time1_4->setStyleSheet("QLabel{color:white};");
    ui->weather1_4->setStyleSheet("QLabel{color:white};");
    ui->tem1_h_4->setStyleSheet("QLabel{color:white};");
    ui->tem1_l_4->setStyleSheet("QLabel{color:white};");
    ui->tem1_y_4->setStyleSheet("QLabel{color:white};");
    QPixmap pax_4;
    pax_4.load(":/picture/main_window/weather/晴天.png");
    ui->picture1_4->setPixmap(pax_4);
    ui->picture1_4->setScaledContents(true);

    pex.setColor(QPalette::Background ,QColor(53,154,225));       //背景色
    ui->label_5->setAutoFillBackground(true);
    ui->label_5->setPalette(pex);
    ui->label_5->setStyleSheet("QLabel{background-color: rgb(53,154,225);border:2px;border-radius:10px;border-color:rgb(53,154,225)};");
    ui->time1_5->setStyleSheet("QLabel{color:white};");
    ui->weather1_5->setStyleSheet("QLabel{color:white};");
    ui->tem1_h_5->setStyleSheet("QLabel{color:white};");
    ui->tem1_l_5->setStyleSheet("QLabel{color:white};");
    ui->tem1_y_5->setStyleSheet("QLabel{color:white};");
    QPixmap pax_5;
    pax_5.load(":/picture/main_window/weather/晴天.png");
    ui->picture1_5->setPixmap(pax_5);
    ui->picture1_5->setScaledContents(true);

/*    QPalette pe;
    pe.setColor(QPalette::Background,Qt::black);
    ui->weather_label->setPalette(pe);
    ui->weather_label->setAutoFillBackground(true);*/  //一定要这句，否则不
}
Map_window::~Map_window()
{
    delete ui;
}
bool Map_window::eventFilter(QObject *obj, QEvent *event)
{
    QPixmap pic;
    if (obj == ui->Back_menu)//指定某个QLabel
     {
         if (event->type() == QEvent::MouseButtonPress) //鼠标点击
         {
             QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event); // 事件转换
             if(mouseEvent->button() == Qt::LeftButton)
             {
                 this->hide();
                 pmain_window->show();
                 return true;
             }
             else
             {
                 return false;
             }
         }
         else
         {
             return false;
         }
     }
    if (obj == ui->change)//指定某个QLabel
     {
         if (event->type() == QEvent::MouseButtonPress) //鼠标点击
         {
             QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event); // 事件转换
             if(mouseEvent->button() == Qt::LeftButton)
             {
                 if(this->city_flag == 0){
                     city_name = "厦门";
                     pmain_window->city_id = "101230201";
                     city_flag=1;

                 }
                 else if(this->city_flag == 1){
                     city_name = "泉州";
                     city_flag=2;
                     pmain_window->city_id = "101230501";
                 }
                 else if(this->city_flag == 2){
                     city_name = "兰州";
                     city_flag=0;
                     pmain_window->city_id = "101160101";
                 }
                 pmain_window->weather_init(pmain_window->city_id);
                 qDebug()<<city_name<<city_flag;
                 return true;
             }
             else
             {
                 return false;
             }
         }
         else
         {
             return false;
         }
     }
}
void Map_window::chang_txt(void){
    QPixmap pax;
    QString low="\0",high="\0";
    QString tem="\0";
    //上半部分
    ui->city->setText(city_name);
    ui->temp_now->setText(temp_now);
    if(tq_flag == 0){
        pax.load(":/picture/main_window/weather/晴天.png");
        ui->tq_pic->setPixmap(pax);
        ui->tq_pic->setScaledContents(true);
        ui->tq_txt->setText("晴天");

        pax.load(":/picture/weather_window/气球.png");
        ui->pict_1->setPixmap(pax);
        ui->pict_1->setScaledContents(true);

        pax.load(":/picture/weather_window/太阳伞.png");
        ui->pict_2->setPixmap(pax);
        ui->pict_2->setScaledContents(true);
    }
    else if(tq_flag == 1){
        pax.load(":/picture/main_window/weather/雨天.png");
        ui->tq_pic->setPixmap(pax);
        ui->tq_pic->setScaledContents(true);
        ui->tq_txt->setText("雨天");

        pax.load(":/picture/weather_window/下雨.png");
        ui->pict_1->setPixmap(pax);
        ui->pict_1->setScaledContents(true);

        pax.load(":/picture/weather_window/面性雨伞.png");
        ui->pict_2->setPixmap(pax);
        ui->pict_2->setScaledContents(true);
    }
    else if(tq_flag == 2){
        pax.load(":/picture/main_window/weather/多云.png");
        ui->tq_pic->setPixmap(pax);
        ui->tq_pic->setScaledContents(true);
        ui->tq_txt->setText("多云");
        pax.load(":/picture/weather_window/天气云朵.png");
        ui->pict_1->setPixmap(pax);
        ui->pict_1->setScaledContents(true);

        pax.load(":/picture/weather_window/烤棉花糖.png");
        ui->pict_2->setPixmap(pax);
        ui->pict_2->setScaledContents(true);
    }
    else if(tq_flag == 3){
        pax.load(":/picture/main_window/weather/阵雪.png");
        ui->tq_pic->setPixmap(pax);
        ui->tq_pic->setScaledContents(true);
        ui->tq_txt->setText("雪天");
        pax.load(":/picture/weather_window/雪球.png");
        ui->pict_1->setPixmap(pax);
        ui->pict_1->setScaledContents(true);

        pax.load(":/picture/weather_window/雪人.png");
        ui->pict_2->setPixmap(pax);
        ui->pict_2->setScaledContents(true);
    }
    else if(tq_flag == 4){
        pax.load(":/picture/main_window/weather/阴天.png");
        ui->tq_pic->setPixmap(pax);
        ui->tq_pic->setScaledContents(true);
        ui->tq_txt->setText("阴天");
        pax.load(":/picture/weather_window/云朵.png");
        ui->pict_1->setPixmap(pax);
        ui->pict_1->setScaledContents(true);

        pax.load(":/picture/weather_window/通风风扇.png");
        ui->pict_2->setPixmap(pax);
        ui->pict_2->setScaledContents(true);
    }
    //下半部分
    ui->time1->setText(day1_time);
    if(day1_tq == 0){
        pax.load(":/picture/main_window/weather/晴天.png");
        ui->picture1->setPixmap(pax);
        ui->picture1->setScaledContents(true);
        ui->weather1->setText("晴天");
    }
    else if(day1_tq == 1){
        pax.load(":/picture/main_window/weather/雨天.png");
        ui->picture1->setPixmap(pax);
        ui->picture1->setScaledContents(true);
        ui->weather1->setText("雨天");
    }
    else if(day1_tq == 2){
        pax.load(":/picture/main_window/weather/多云.png");
        ui->picture1->setPixmap(pax);
        ui->picture1->setScaledContents(true);
        ui->weather1->setText("多云");
    }
    else if(day1_tq == 3){
        pax.load(":/picture/main_window/weather/阵雪.png");
        ui->picture1->setPixmap(pax);
        ui->picture1->setScaledContents(true);
        ui->weather1->setText("雪天");
    }
    else if(day1_tq == 4){
        pax.load(":/picture/main_window/weather/阴天.png");
        ui->picture1->setPixmap(pax);
        ui->picture1->setScaledContents(true);
        ui->weather1->setText("阴天");
    }

    ui->tem1_l->setText(day1_l);
    ui->tem1_h->setText(day1_h);

    ui->time1_2->setText(day2_time);
    if(day2_tq == 0){
        pax.load(":/picture/main_window/weather/晴天.png");
        ui->picture1_2->setPixmap(pax);
        ui->picture1_2->setScaledContents(true);
        ui->weather1_2->setText("晴天");
    }
    else if(day2_tq == 1){
        pax.load(":/picture/main_window/weather/雨天.png");
        ui->picture1_2->setPixmap(pax);
        ui->picture1_2->setScaledContents(true);
        ui->weather1_2->setText("雨天");
    }
    else if(day2_tq == 2){
        pax.load(":/picture/main_window/weather/多云.png");
        ui->picture1_2->setPixmap(pax);
        ui->picture1_2->setScaledContents(true);
        ui->weather1_2->setText("多云");
    }
    else if(day2_tq == 3){
        pax.load(":/picture/main_window/weather/阵雪.png");
        ui->picture1_2->setPixmap(pax);
        ui->picture1_2->setScaledContents(true);
        ui->weather1_2->setText("雪天");
    }

    else if(day2_tq == 4){
        pax.load(":/picture/main_window/weather/阴天.png");
        ui->picture1_2->setPixmap(pax);
        ui->picture1_2->setScaledContents(true);
        ui->weather1_2->setText("阴天");
    }
    ui->tem1_l_2->setText(day2_l);
    ui->tem1_h_2->setText(day2_h);


    ui->time1_3->setText(day3_time);
    if(day3_tq == 0){
        pax.load(":/picture/main_window/weather/晴天.png");
        ui->picture1_3->setPixmap(pax);
        ui->picture1_3->setScaledContents(true);
        ui->weather1_3->setText("晴天");
    }
    else if(day3_tq == 1){
        pax.load(":/picture/main_window/weather/雨天.png");
        ui->picture1_3->setPixmap(pax);
        ui->picture1_3->setScaledContents(true);
        ui->weather1_3->setText("雨天");
    }
    else if(day3_tq == 2){
        pax.load(":/picture/main_window/weather/多云.png");
        ui->picture1_3->setPixmap(pax);
        ui->picture1_3->setScaledContents(true);
        ui->weather1_3->setText("多云");
    }
    else if(day3_tq == 3){
        pax.load(":/picture/main_window/weather/雪天.png");
        ui->picture1_3->setPixmap(pax);
        ui->picture1_3->setScaledContents(true);
        ui->weather1_3->setText("雪天");
    }

    else if(day1_tq == 4){
        pax.load(":/picture/main_window/weather/阴天.png");
        ui->picture1_3->setPixmap(pax);
        ui->picture1_3->setScaledContents(true);
        ui->weather1_3->setText("阴天");
    }
    ui->tem1_l_3->setText(day3_l);
    ui->tem1_h_3->setText(day3_h);


    ui->time1_4->setText(day4_time);
    if(day4_tq == 0){
        pax.load(":/picture/main_window/weather/晴天.png");
        ui->picture1_4->setPixmap(pax);
        ui->picture1_4->setScaledContents(true);
        ui->weather1_4->setText("晴天");
    }
    else if(day4_tq == 1){
        pax.load(":/picture/main_window/weather/雨天.png");
        ui->picture1_4->setPixmap(pax);
        ui->picture1_4->setScaledContents(true);
        ui->weather1_4->setText("雨天");
    }
    else if(day4_tq == 2){
        pax.load(":/picture/main_window/weather/多云.png");
        ui->picture1_4->setPixmap(pax);
        ui->picture1_4->setScaledContents(true);
        ui->weather1_4->setText("多云");
    }
    else if(day4_tq == 3){
        pax.load(":/picture/main_window/weather/阵雪.png");
        ui->picture1_4->setPixmap(pax);
        ui->picture1_4->setScaledContents(true);
        ui->weather1_4->setText("雪天");
    }

    else if(day1_tq == 4){
        pax.load(":/picture/main_window/weather/阴天.png");
        ui->picture1_4->setPixmap(pax);
        ui->picture1_4->setScaledContents(true);
        ui->weather1_4->setText("阴天");
    }
    ui->tem1_l_4->setText(day4_l);
    ui->tem1_h_4->setText(day4_h);


    ui->time1_5->setText(day5_time);
    if(day5_tq == 0){
        pax.load(":/picture/main_window/weather/晴天.png");
        ui->picture1_5->setPixmap(pax);
        ui->picture1_5->setScaledContents(true);
        ui->weather1_5->setText("晴天");
    }
    else if(day5_tq == 1){
        pax.load(":/picture/main_window/weather/雨天.png");
        ui->picture1_5->setPixmap(pax);
        ui->picture1_5->setScaledContents(true);
        ui->weather1_5->setText("雨天");
    }
    else if(day5_tq == 2){
        pax.load(":/picture/main_window/weather/多云.png");
        ui->picture1_5->setPixmap(pax);
        ui->picture1_5->setScaledContents(true);
        ui->weather1_5->setText("多云");
    }
    else if(day5_tq == 3){
        pax.load(":/picture/main_window/weather/阵雪.png");
        ui->picture1_5->setPixmap(pax);
        ui->picture1_5->setScaledContents(true);
        ui->weather1_5->setText("雪天");
    }

    else if(day1_tq == 4){
        pax.load(":/picture/main_window/weather/阴天.png");
        ui->picture1_5->setPixmap(pax);
        ui->picture1_5->setScaledContents(true);
        ui->weather1_5->setText("阴天");
    }
    ui->tem1_l_5->setText(day5_l);
    ui->tem1_h_5->setText(day5_h);
}
void Map_window::paintEvent(QPaintEvent *event){

    QPainter painter(this);
    QRect rect2(0,240,800,240);
    painter.setPen(QPen(QColor(94,174,255, 0), 1));
    painter.setBrush(QColor(94,174,255));
    painter.drawRect(rect2);

    // 反走样
    painter.setRenderHint(QPainter::Antialiasing, true);
    // 设置渐变色,设置起点,终点
    QLinearGradient linear(100,100,100,200);
    linear.setColorAt(0, QColor(254, 202, 63));
    linear.setColorAt(1, QColor(254, 218, 124));
    // 设置显示模式
    linear.setSpread(QGradient::PadSpread);
    // 设置画笔颜色、宽度
    painter.setPen(QPen(QColor(255, 255, 255, 0), 1));
    QRect rect(0,0,800,240);
    // 设置画刷填充
    painter.setBrush(QBrush(linear));
    painter.drawRect(rect);
}
